#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
#define inf 0x5fffffff
#define maxn 100005
#define parent(i) (i / 2)
#define lchild(i) (2 * i)
#define rchild(i) (2 * i + 1)
int n, val;
long long total;
int heap[maxn], hsize;
void push(int val)
{
    heap[++hsize] = val;
    int now = hsize, par = parent(now);
    while (now >= 1) //UpTune
    {
        par = parent(now);
        if (heap[now] >= heap[par] || par == 0)
            break;
        swap(heap[now], heap[par]);
        now = par;
    }
}
int top() { return heap[1]; };
int pop()
{
    int elem = heap[1];
    heap[1] = heap[hsize];
    heap[hsize--] = inf;
    int now = 1, l, r;
    while (now <= hsize) //DownTune
    {
        l = lchild(now);
        r = rchild(now);
        int tmp = heap[l] < heap[r] ? l : r;
        tmp = heap[tmp] < heap[now] ? tmp : now;
        if (tmp == now)
            break;
        swap(heap[tmp], heap[now]);
        now = tmp;
    }

    return elem;
}
void initHeap()
{
    fill(heap, heap + maxn, inf);
    hsize = 0;
};
int main()
{
    initHeap();
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
    {
        scanf("%d", &val);
        push(val);
    }
    val = 0, total = 0;
    while (hsize != 0)
    {
        val = 0;
        for (int i = 0; i < 2; ++i)
            if (hsize != 0)
                val += pop();
        total += val;
        if (hsize == 0)
            break;
        else
            push(val);
    }
    printf("%lld", total);
    return 0;
}